<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 优化
      function printAnimalDetails(animal) {
        var result = null
        if (animal) {
          if (animal.type) {
            if (animal.name) {
              if (animal.gender) {
                result = `${animal.name} is a ${animal.gender} - ${animal.type}`
              } else {
                result = 'no animal gender'
              }
            } else {
              result = 'no animal name'
            }
          } else {
            result = 'no animal type'
          }
        } else {
          result = 'no animal'
        }
        return result
      }

      // 提前退出 和 提前返回
      const printAnimalDetail = ({ type, name, gender }) => {
        if (!type) return 'no animal type'
        if (!name) return 'no animal name'
        if (!gender) return 'no animal gender'
        return `${name} is a ${gender} - ${type}`
      }

      console.log(printAnimalDetail({ name: '大黄' }))
      console.log(printAnimalDetail({ name: '大黄', type: 'dog', gender: 'male' }))
    </script>
  </body>
</html>
